rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer",
             "fastDummies","ICCbin","ihs","readstata13","xtable")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages, new.packages)

setwd("PUT YOUR DIRECTORY HERE")
setwd("/Users/hagerans/Dropbox/Research/Polish_priests/8_Replicationn/replication")


## load data
data <- read.csv("./Datasets/main_confounders.csv")

## rearrange
data <- data%>%
  dplyr::mutate(across(subbotnik_inkind_1975:subbotnik_inkind_1979, ~scale(.x)))%>%
  dplyr::rowwise()%>%
  dplyr::mutate(commander_1975_79 = mean(c(commanders1975, commanders1976, 
                                           commanders1977, commanders1978,
                                           commanders1979), na.rm = TRUE))%>%
  dplyr::mutate(compliance = mean(c(subbotnik_inkind_1975, subbotnik_inkind_1976, 
                                    subbotnik_inkind_1977, subbotnik_inkind_1978,
                                    subbotnik_inkind_1979), na.rm = TRUE))%>%
  dplyr::ungroup()%>%
  dplyr::mutate(commander_1975_79 = scale(commander_1975_79),
                strikes = scale(strikes),
                delegates = scale(delegates),
                sabotage = scale(compliance * -1))

## run model
lm_sabotage <- lm(sabotage ~ commander_1975_79 + delegates, data = data)

## output in tex

stargazer(lm_sabotage, 
          dep.var.labels = c("Sabotage"),
          style = "qje",
          covariate.labels = c("Surveillance", "Local Solidarnosc strength"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant"),
          omit.stat = c("rsq", "ser"), 
          omit.table.layout = "n",
          out = "PUT YOUR FILEPATH HERE")

summary(lm_sabotage)
